<?php
/**
 * WQPHP封装工具类，仅供学习研究使用，禁止任何形式的转载发布
 * User: Jessica@aweb.wang
 * Date: 2023/3/10 14:50
 * Email: <1194323013@qq.com>
 *                            _ooOoo_
 *                           o8888888o
 *                           88" . "88
 *                           (| -_- |)
 *                           O\  =  /O
 *                        ____/`---'\____
 *                      .'  \\|     |//  `.
 *                     /  \\|||  :  |||//  \
 *                    /  _||||| -:- |||||-  \
 *                    |   | \\\  -  /// |   |
 *                    | \_|  ''\---/''  |   |
 *                    \  .-\__  `-`  ___/-. /
 *                  ___`. .'  /--.--\  `. . __
 *               ."" '<  `.___\_<|>_/___.'  >'"".
 *              | | :  `- \`.;`\ _ /`;.`/ - ` : | |
 *              \  \ `-.   \_ __\ /__ _/   .-` /  /
 *         ======`-.____`-.___\_____/___.-`____.-'======
 *                            `=---='
 *        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 *                      Buddha Bless, No Bug !
 **/

namespace app\common\utils;


use app\common\exception\BusinessException;
use app\common\exception\BusinessExceptionEnum;
use Qiniu\Auth;
use think\facade\Config;
use think\facade\Request;
use Qiniu\Storage\UploadManager;

class UploadUtils
{

    public static function thumbupload($file)
    {

        if ($file==null) {
            throw new BusinessException(BusinessExceptionEnum::FILE_EMPTY);
        }
        else{

            $temp = explode(".", $_FILES["file"]["name"]);
            $extension = end($temp);
            // 判断文件是否合法
            if(!in_array($extension, array("gif","jpeg","jpg","png"))){
                throw new BusinessException(BusinessExceptionEnum::FILE_WRONG);
            }else{
                $qiniuyunStatus=Config::get('app.qiniuyun_status');
                if($qiniuyunStatus){
                    $qiniuyunAk=Config::get('app.qiniuyun_ak');
                    $qiniuyunSk=Config::get('app.qiniuyun_sk');
                    $qiniuyunBucket=Config::get('app.qiniuyun_bucket');
                    $qiniuyunDomain=Config::get('app.qiniuyun_domain');
                    $auth=new Auth($qiniuyunAk,$qiniuyunSk);
                    //生成上传TOKEN
                    $token=$auth->uploadToken($qiniuyunBucket);
                    $uploadMgr=new UploadManager();
                    $path=md5(microtime(true).mt_rand(1,1e9)).'.'.$extension;
                    $info=$uploadMgr->putFile($token,$path,$file->getRealPath());
                    $data['sendurl']=$qiniuyunDomain.'/'.$info[0]['key'];
                    $data['savename']=$qiniuyunDomain.'/'.$info[0]['key'];

                }else{
                    $savename = \think\facade\Filesystem::disk('public')->putFile( '/pic', $file);
                    $savename=str_replace('\\', '/', $savename);
                    $data['sendurl']='/storage/'.$savename;
                    $data['savename']=$savename;
                }
                $data['ext']=$extension;

                return $data;
            }
        }
    }
    public static function Excelupload($file,$path)
    {
            if ($file==null) {
                throw new BusinessException(BusinessExceptionEnum::FILE_EMPTY);
            }
            else{

                $temp = explode(".", $_FILES["file"]["name"]);
                $extension = end($temp);
                // 判断文件是否合法
                if(!in_array($extension, array('xls','xlsx'))){
                    throw new BusinessException(BusinessExceptionEnum::FILE_WRONG);
                }else{
                    $savename = \think\facade\Filesystem::disk('public')->putFile( $path, $file);
                    $savename=str_replace('\\', '/', $savename);
                    $data['sendurl']=app()->getRootPath().'/public/storage/'.$savename;
                    $data['savename']=$savename;
                    $data['ext']=$extension;
                    return $data;
                }

        }


    }
}